//904. 水果成篮   https://leetcode.cn/problems/fruit-into-baskets/
class Solution {
    public int totalFruit(int[] fruits) {
        int n = fruits.length;
        //创建一个哈希表存储水果种类和数量
        Map<Integer, Integer> cnt = new HashMap<Integer, Integer>();
        //创建左右指针
        int left = 0;
        //记录最大数量
        int maxcount = 0;

        //开始滑动
        for (int right = 0; right < n; right++) {
            //直接向右滑动
            cnt.put(fruits[right], cnt.getOrDefault(fruits[right], 0) + 1);
            //判断窗口内部是否满足条件

            while (cnt.size() > 2) {
                //不满足条件时一直滑动左窗口
                cnt.put(fruits[left], cnt.getOrDefault(fruits[left], 0) - 1);
                //如果key对应的value为0 ， 则删除key
                if (cnt.get(fruits[left]) == 0) {
                    cnt.remove(fruits[left]);
                }
                left++;
            }
            maxcount = Math.max(maxcount, right - left + 1);
        }
        return maxcount;
    }

}

//LCR 015. 找到字符串中所有字母异位词  https://leetcode.cn/problems/VabMRr/

class Solution {
    public List<Integer> findAnagrams(String s, String p) {
        int arr1[] = new int[26];
        int arr2[] = new int[26];

        List list = new ArrayList<Integer>();
        if(s.length()<p.length()){
            return list;
        }
        for(int i=0;i<p.length();i++){
            arr1[p.charAt(i)-'a']++;
            arr2[s.charAt(i)-'a']++;
        }
        if(Arrays.equals(arr1,arr2)){
            list.add(0);
        }
        for(int i = p.length();i<s.length();i++){
            arr2[s.charAt(i-p.length())-'a']--;
            arr2[s.charAt(i)-'a']++;
             if(Arrays.equals(arr1,arr2)){
                list.add(i-p.length()+1);
        }
        }
        return list;
    }
}

//905. 按奇偶排序数组  https://leetcode.cn/problems/sort-array-by-parity/description/

class Solution {
    public int[] sortArrayByParity(int[] nums) {
        int left = 0;
        int right = nums.length-1;
        while(left <= right){
            while(left<=right&&nums[left]%2==0){
                left++;
            }
            while(left<=right&&nums[right]%2==1){
                right--;
            }
            if(left <= right){
                int tmp = nums[left];
                nums[left] = nums[right];
                nums[right] = tmp;
            }
        }
        return nums;
    }
   
}